package bing.netty.example.time;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class TmeClient {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		recevieData(args);
	}

	public static void recevieData(String[] args) throws Exception{
//		String host=args[0];
//		int port =Integer.parseInt(args[1]);
		String host="localhost";
		int port =8030;
		EventLoopGroup workerGroup=new NioEventLoopGroup();
		
		try{
			Bootstrap bs=new Bootstrap();
			bs.group(workerGroup)
			  .channel(NioSocketChannel.class)
			  .option(ChannelOption.SO_KEEPALIVE, true)
			  .handler(new ChannelInitializer<SocketChannel>(){
				@Override
				protected void initChannel(SocketChannel ch) throws Exception {
					// TODO Auto-generated method stub
					//ch.pipeline().addLast(new TimeClientHandler());//简单模式
					ch.pipeline().addLast(new TimeDecoder(),new TimeClientHandler());//解决Socket Buffer问题
				}
				  
			  });
			//start the client
			ChannelFuture future=bs.connect(host, port).sync();
			
			//wait until the connection is closed
			future.channel().closeFuture().sync();
		}finally{
			workerGroup.shutdownGracefully();
		}
	}
}
